<template>
    <div>
        <!--搜索条件-->
        <el-form :inline="true" :model="searchForm">
            <el-form-item>
                <el-input v-model="searchForm.name" placeholder="名称" clearable></el-input>
            </el-form-item>
            <el-form-item>
                <el-button @click="getRoleList">搜索</el-button>
            </el-form-item>
            <el-form-item>
                <el-button type="primary" @click="dialogVisible = true">新增</el-button>
            </el-form-item>
            <el-form-item>
                <el-popconfirm title="确定要批量删除吗？" @confirm="delHandle(null)">
                    <el-button type="danger" slot="reference" :disabled="btnStatus">批量删除</el-button>
                </el-popconfirm>
            </el-form-item>
        </el-form>
        <!--多选表格-->
        <el-table
                ref="multipleTable"
                :data="tableData"
                tooltip-effect="dark"
                style="width: 100%"
                border
                stripe
                :header-cell-style="{'text-align':'center'}"
                @selection-change="handleSelectionChange">
            <el-table-column
                    type="selection"
                    width="55">
            </el-table-column>
            <el-table-column
                    prop="name"
                    label="名称"
                    width="120">
            </el-table-column>
            <el-table-column
                    prop="code"
                    label="唯一编码"
                    show-overflow-tooltip>
            </el-table-column>
            <el-table-column
                    prop="remark"
                    label="描述"
                    show-overflow-tooltip>
            </el-table-column>
            <el-table-column
                    prop="status"
                    width="80"
                    align="center"
                    label="状态">
                <template slot-scope="scope">
                    <el-tag size="medium" type="success" v-if="scope.row.status === 1">正常</el-tag>
                    <el-tag size="medium" type="danger" v-else-if="scope.row.status === 0">禁用</el-tag>
                </template>
            </el-table-column>
            <el-table-column
                    align="center"
                    label="操作">
                <template slot-scope="scope">
                    <el-button type="text" @click="permHandle(scope.row.id)">分配权限</el-button>
                    <el-divider direction="vertical"></el-divider>
                    <el-button type="text" @click="editForm(scope.row.id)">编辑</el-button>
                    <el-divider direction="vertical"></el-divider>
                    <el-popconfirm title="这是一段内容确定删除吗？" @confirm="delHandle(scope.row.id)">
                        <el-button type="text" slot="reference">删除</el-button>
                    </el-popconfirm>
                </template>
            </el-table-column>
        </el-table>
        <!--分页-->
        <el-pagination
                @size-change="handleSizeChange"
                @current-change="handleCurrentChange"
                layout="total, sizes, prev, pager, next, jumper"
                :page-sizes="[10, 20, 50, 100]"
                :current-page="current"
                :page-size="size"
                :total="total">
        </el-pagination>
        <!--新增/修改对话框-->
        <el-dialog
                title="提示"
                :visible.sync="dialogVisible"
                width="40%"
                :before-close="handleClose">
            <el-form :model="addForm" :rules="rules" ref="addForm" label-width="100px" class="demo-addForm">
                <el-form-item label="角色名称" prop="name">
                    <el-input v-model="addForm.name" placeholder="请输入角色名称" clearable style="width: 60vh"></el-input>
                </el-form-item>
                <el-form-item label="唯一编码" prop="code">
                    <el-input v-model="addForm.code" placeholder="请输入唯一编码" clearable style="width: 60vh"></el-input>
                </el-form-item>
                <el-form-item label="描述" prop="remark">
                    <el-input v-model="addForm.remark" placeholder="请输入描述" clearable style="width: 60vh"></el-input>
                </el-form-item>
                <el-form-item label="状态" prop="status">
                    <el-radio-group v-model="addForm.status">
                        <el-radio :label=1>正常</el-radio>
                        <el-radio :label=0>禁用</el-radio>
                    </el-radio-group>
                </el-form-item>
            </el-form>
            <div slot="footer" class="dialog-footer">
                <el-button @click="resetForm()">取 消</el-button>
                <el-button type="primary" @click="submitForm('addForm')">确 定</el-button>
            </div>
        </el-dialog>
        <!--分配权限对话框-->
        <el-dialog
                title="分配权限"
                :visible.sync="permDialogVisible"
                width="40%">
            <div class="dialogDiv">
                <el-form :model="permForm">
                    <el-tree
                            :data="permData"
                            show-checkbox
                            node-key="id"
                            default-expand-all
                            ref="tree"
                            :check-strictly="true"
                            :props="defaultProps">
                    </el-tree>
                </el-form>
            </div>
            <span slot="footer" class="dialog-footer">
                <el-button @click="permDialogVisible = false">取 消</el-button>
                <el-button type="primary" @click="submitPerm">确 定</el-button>
            </span>
        </el-dialog>
    </div>
</template>

<script>
    export default {
        name: 'Role',
        data() {
            return {
                searchForm: {},
                tableData: [],
                multipleSelection: [],
                total: 0,
                current: 1,
                size: 10,
                dialogVisible: false,
                addForm: {
                    status: 1
                },
                rules: {
                    name: [
                        {required: true, message: '请输入角色名称', trigger: 'blur'}
                    ],
                    code: [
                        {required: true, message: '请输入唯一编码', trigger: 'blur'}
                    ],
                    status: [
                        {required: true, message: '请选择状态', trigger: 'change'}
                    ]
                },
                btnStatus: true,
                permDialogVisible: false,
                permForm: {},
                defaultProps: {
                    children: 'children',
                    label: 'name'
                },
                permData: []
            }
        },
        created() {
            this.getRoleList();
            // 查询菜单列表
            this.$axios.get('/rookie/menu/list').then(res => {
                this.permData = res.data.result;
            });
        },
        methods: {
            // 点击多选框
            handleSelectionChange(val) {
                this.multipleSelection = val;
                this.btnStatus = val.length === 0;
            },
            // 每页的条数
            handleSizeChange(val) {
                this.size = val;
                this.getRoleList();
            },
            // 上一页/下一页/第几页
            handleCurrentChange(val) {
                this.current = val;
                this.getRoleList();
            },
            // 关闭对话框
            handleClose() {
                this.resetForm('addForm');
            },
            // 重置
            resetForm() {
                this.addForm = {
                    status: 1
                };
                this.dialogVisible = false;
            },
            // 新增/修改
            submitForm(formName) {
                this.$refs[formName].validate((valid) => {
                    if (valid) {
                        this.$axios.post('/rookie/role/' + (this.addForm.id ? 'update' : 'save'), this.addForm).then(res => {
                            this.$message({
                                showClose: true,
                                message: '恭喜你，操作成功',
                                type: 'success',
                                center: true,
                                onClose: () => {
                                    this.getRoleList();
                                }
                            });
                            this.resetForm();
                        });
                    } else return false;
                });
            },
            // 获取角色列表
            getRoleList() {
                this.$axios.get('/rookie/role/list', {
                    params: {
                        name: this.searchForm.name,
                        current: this.current,
                        size: this.size
                    }
                }).then(res => {
                    this.tableData = res.data.result.records;
                    this.total = res.data.result.total;
                    this.current = res.data.result.current;
                    this.size = res.data.result.size;
                });
            },
            // 点击修改按钮
            editForm(id) {
                this.$axios.get('/rookie/role/getInfoById/' + id).then(res => {
                    this.addForm = res.data.result;
                    this.dialogVisible = true;
                });
            },
            // 删除
            delHandle(id) {
                let ids = [];
                if (id) {
                    ids.push(id);
                } else {
                    this.multipleSelection.forEach(row => {
                        ids.push(row.id);
                    })
                }
                this.$axios.post('/rookie/role/delete', ids).then(res => {
                    this.$message({
                        showClose: true,
                        message: '恭喜你，操作成功',
                        type: 'success',
                        center: true,
                        onClose: () => {
                            this.getRoleList();
                        }
                    });
                });
            },
            // 点击分配权限按钮
            permHandle(id) {
                this.permDialogVisible = true;
                // 根据角色的id查询该角色的权限
                this.$axios.get('/rookie/role/getInfoById/' + id).then(res => {
                    this.$refs.tree.setCheckedKeys(res.data.result.menuIdsList);
                    this.permForm = res.data.result;
                });
            },
            // 提交权限
            submitPerm() {
                let menuIds = this.$refs.tree.getCheckedKeys();
                this.$axios.post('/rookie/role/AssignPermissions/' + this.permForm.id, menuIds).then(res => {
                    this.$message({
                        showClose: true,
                        message: '恭喜你，操作成功',
                        type: 'success',
                        center: true,
                        onClose: () => {
                            this.getRoleList();
                        }
                    });
                    this.permDialogVisible = false;
                });
            }
        }
    }
</script>

<style>
    .el-pagination {
        float: right;
        margin-top: 20px;
    }

    .dialogDiv {
        height: 50vh;
        overflow: auto;
    }
</style>